<?php

namespace App\Policies;

use App\Models\Order;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class OrderPolicy
{
    use HandlesAuthorization;

    /**
     * 确定用户是否可以查看订单
     */
    public function view(User $user, Order $order): bool
    {
        return $user->id === $order->user_id;
    }

    /**
     * 确定用户是否可以更新订单
     */
    public function update(User $user, Order $order): bool
    {
        return $user->id === $order->user_id;
    }

    /**
     * 确定用户是否可以取消订单
     */
    public function cancel(User $user, Order $order): bool
    {
        return $user->id === $order->user_id && $order->canCancel();
    }

    /**
     * 确定用户是否可以确认收货
     */
    public function confirmDelivery(User $user, Order $order): bool
    {
        return $user->id === $order->user_id && $order->canConfirmDelivery();
    }

    /**
     * 确定用户是否可以申请售后
     */
    public function applyAfterSales(User $user, Order $order): bool
    {
        return $user->id === $order->user_id && $order->canApplyAfterSales();
    }

    /**
     * 确定用户是否可以支付订单
     */
    public function pay(User $user, Order $order): bool
    {
        return $user->id === $order->user_id && $order->canPay();
    }

    /**
     * 确定管理员是否可以管理订单
     */
    public function manage(User $user, Order $order): bool
    {
        return $user->hasRole('admin') || $user->hasRole('super_admin');
    }

    /**
     * 确定管理员是否可以发货
     */
    public function ship(User $user, Order $order): bool
    {
        return $this->manage($user, $order) && $order->canShip();
    }

    /**
     * 确定管理员是否可以处理退款
     */
    public function refund(User $user, Order $order): bool
    {
        return $this->manage($user, $order) && $order->canRefund();
    }
}